package com.threeti.mecool.core.infrastructure.persistence.project;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import com.threeti.mecool.core.domain.model.project.ProjectEmployee;

public interface ProjectEmployeeRepository extends JpaRepository<ProjectEmployee, Long> ,JpaSpecificationExecutor<ProjectEmployee> {
  @Modifying
  @Transactional
  @Query("delete from ProjectEmployee pe where pe.project.id = ?1")  
  void deleteByProjectId(Long projectId);
  
  List<ProjectEmployee> findByProjectId(Long projectId);
  
  ProjectEmployee findByProjectCodeAndEmpId(String projectCode, Long empId);
  
  //@Query("from ProjectEmployee where parent.empId = ?1 and order by project.code")  
  List<ProjectEmployee> findByParentEmpIdAndProjectId(Long parentEmpId, Long projId);

  
  @Query(nativeQuery=true,value="select distinct t.title_within_the_project from DM_PROJECT_EMPLOYEE t where t.project = ?1 and t.title_within_the_project is not null order by t.title_within_the_project")
  List<String> findAllTitles(long projectId);
  
  
  @Query(nativeQuery=true,value="select t2.code from DM_PROJECT_EMPLOYEE t1,DM_PROJECT t2 where t1.emp_id= ?1 and t1.project=t2.id")
  List<String> findOwnProjectIds(Long empId);
  
}
